;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Sales.BorrowIn", [])
        .controller("BorrowInCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "UserContextService", "Upload", "$timeout", "EventBus", "$filter", "$stateParams", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, UserContextService, Upload, $timeout, EventBus, $filter, $stateParams) {
            $scope.activeFlag = [true, false, false];
            $scope.detailFlag = [true, true];
            $scope.markInfo = {
                allCheckedBill: false,
                allCheckedMaterial: false,
                curRow: -1
            };
            $scope.showDatepicker1 = false;
            $scope.showDatepicker2 = false;
            $scope.submitted = false;
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.condition = {};
            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false,
                showMateriel: false
            };
            $scope.dataList = [];
            $scope.deliveryWayList = [];
            $scope.detailInfo = {
                crucialDevice: 0,
                entranceDevice: 0
            };
            $scope.tabFlag = {};
            var tabFlag = {
                0: "详情",
                1: "新增",
                2: "编辑"
            };
            $scope.exportParam = {
                sectionUrl: ybHttp.RequestUrl("device", "/borrowMaterial/export"),
                sectionParam: {
                    ids: []
                },
                allUrl: ybHttp.RequestUrl("device", "/borrowMaterial/exportAll")
            };
            $scope.orderObj = {
                id: '',
                code: '',
                serviceInformationMember: '',
                crantNo: '',
                warehouseLeadIn: '',
                warehouseAreaLeadIn: '',
                borrowReasonDesc: '',
                remarks: '',
                expectedArrivalDate: '',
                factoryNumber: '',
                borrower: '',
                createdDate: '',
                batchNo: '',
                oneLevelAuditStatus: '', // 一级审核状态
                twoLevelAuditStatus: '', // 二级审核状态
                linkMan: '',
                linkPhone: '',
                isBorrow: '',
                auditOpinion: '',
                auditRemarks: '',
                borrowMaterialDetailDOList: []
            };
            $scope.isSap = 0;
            $scope.isBorrowType = [{
                    code: '0',
                    name: '拆'
                },
                {
                    code: '1',
                    name: '借'
                }
            ]; // 拆、借
            $scope.tableConfig = {
                fixedWidth: 185,
                scrollWidth: 1800,
                tableCol: [{
                        type: "",
                        name: "借用单号",
                        dataField: "code",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        clickFn: "viewDetail",
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "服务信息单号",
                        dataField: "serviceInformationMember",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "吊机编号",
                        dataField: "crantNo",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "领入仓库",
                        dataField: "warehouseLeadIn",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "领入库区",
                        dataField: "warehouseAreaLeadIn",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "借拆件原因说明",
                        dataField: "borrowReasonDesc",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "一级审核状态",
                        dataField: "oneLevelAuditStatusShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "二级审核状态",
                        dataField: "twoLevelAuditStatusShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "入库状态", // 新增入库状态字段
                        dataField: "status",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "备注",
                        dataField: "remarks",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "一级审核时间",
                        dataField: "oneLevelAuditDate",
                        width: 140,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "二级审核时间",
                        dataField: "twoLevelAuditDate",
                        width: 140,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "入库时间",
                        dataField: "inboundDate",
                        width: 140,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "出库时间",
                        dataField: "deliveryDate",
                        width: 140,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "归还时间",
                        dataField: "returnDate",
                        width: 140,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "预计归还日期",
                        dataField: "expectedArrivalDateShow", // 日期格式转换
                        width: 130,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "出厂编号",
                        dataField: "factoryNumber",
                        width: 130,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "借用人",
                        dataField: "borrower",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "创建日期",
                        dataField: "createdDateShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "批次号",
                        dataField: "batchNo",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "operate",
                        name: "操作",
                        dataField: "",
                        width: 145,
                        isShow: true,
                        isFixed: true,
                        toSort: false,
                        sortType: ""
                    }
                ]
            };
            $scope.materielTableConfig = {
                scrollWidth: 870,
                tableCol: [{
                        name: "零件编号",
                        width: 150,
                        dataField: "materialNo",
                        needInput: false
                    },
                    {
                        name: "零件中文名",
                        width: 150,
                        dataField: "materialDesc",
                        needInput: false
                    },
                    {
                        name: "数量",
                        width: 100,
                        dataField: "amount",
                        needInputNumber: true
                    },
                    {
                        name: "备注",
                        width: 200,
                        dataField: "remarks",
                        needInput: true
                    },
                ]
            };

            var btnPermissionCode = {
                borrowInAdd: window.ActionCode.borrowInAdd,
                borrowInDel: window.ActionCode.borrowInDel,
                borrowInAudit: window.ActionCode.borrowInAudit,
                borrowInEdit: window.ActionCode.borrowInEdit,
                borrowInPrint: window.ActionCode.borrowInPrint,
                borrowInExport: window.ActionCode.borrowInExport,
                borrowInExportAll: window.ActionCode.borrowInExportAll,

            };

            $scope.btnPermission = {
                borrowInAdd: true,
                borrowInDel: true,
                borrowInAudit: true,
                borrowInEdit: true,
                borrowInPrint: true,
                borrowInExport: true,
                borrowInExportAll: true,
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            $scope.setFixedWidth = function () {
                $scope.tbStyle = {
                    billFixedWidth: {
                        left: $scope.tableConfig.fixedWidth + "px"
                    }
                };
            };

            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false, false];
                $scope.activeFlag[index] = true;
                $scope.submitted = false;
                switch (index) {
                    case 0:
                        initData();
                        getDataList();
                        $scope.submitted = false;
                        break;
                    case 1:
                        break;
                    case 2:
                        break;
                    case 4:
                        break;
                    default:
                        break;
                }
            };
            function initData() {
                $scope.tabFlag = {};
                $scope.detailInfo = {
                    crucialDevice: 0,
                    entranceDevice: 0
                };
                $scope.orderObj = {
                    code: '',
                    serviceInformationMember: '',
                    crantNo: '',
                    warehouseLeadIn: '',
                    warehouseAreaLeadIn: '',
                    borrowReasonDesc: '',
                    remarks: '',
                    expectedArrivalDate: '',
                    factoryNumber: '',
                    borrower: '',
                    oneLevelAuditStatus: '', // 一级审核状态
                    twoLevelAuditStatus: '', // 二级审核状态
                    createdDate: '',
                    batchNo: '',
                    linkMan: '',
                    linkPhone: '',
                    isBorrow: '',
                    auditOpinion: '',
                    auditRemarks: '',
                    borrowMaterialDetailDOList: []
                };
                $scope.isSap = 0;
                $scope.markInfo.allCheckedMaterial = false;
                $scope.toolbarConfig.showMateriel = false;
                $scope.detailFlag = [true, true];
            }


            /* 获取主列表数据 */
            function getDataList(curPage) {
                $scope.markInfo = {
                    allCheckedBill: false,
                    allCheckedMaterial: false,
                    curRow: -1
                };
                $scope.exportParam.sectionParam.ids = [];
                var service = "device";
                var reqUrl = "/borrowMaterial/list";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = $scope.condition;
                return ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.isChecked = false;
                        item.expectedArrivalDateShow = $filter("YbDate")(item.expectedArrivalDate, 'yyyy-MM-dd HH:mm:ss');
                        item.oneLevelAuditDate = $filter("YbDate")(item.oneLevelAuditDate, "yyyy-MM-dd HH:mm:ss");
                        item.twoLevelAuditDate = $filter("YbDate")(item.twoLevelAuditDate, "yyyy-MM-dd HH:mm:ss");
                        item.inboundDate = $filter("YbDate")(item.inboundDate, "yyyy-MM-dd HH:mm:ss");
                        item.returnDate = $filter("YbDate")(item.returnDate, "yyyy-MM-dd HH:mm:ss");
                        item.deliveryDate = $filter("YbDate")(item.deliveryDate, "yyyy-MM-dd HH:mm:ss");
                        // item.returnDate = $filter("YbDate")(item.returnDate, "yyyy-MM-dd HH:mm:ss");
                        item.createdDateShow = $filter("YbDate")(item.createdDate, 'yyyy-MM-dd HH:mm:ss');
                        item.status = $filter("ProgressStatus")(item.status, "inStore");
                        item.oneLevelAuditStatusShow = $filter("ProgressStatus")(item.oneLevelAuditStatus, "audit");
                        item.twoLevelAuditStatusShow = $filter("ProgressStatus")(item.twoLevelAuditStatus, "audit");
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                    return resp.value.list;
                });
            }

            /* 主列表点击申请单号触发 */
            $scope.tdClick = function (item, fnType) {
                switch (fnType) {
                    case "viewDetail": // 查看详情
                        viewDetail(item);
                        break;
                    default:
                        break;
                }
            };


            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.cueRow = index
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.reset = function () {
                $scope.condition = {};
            };


            /* 删除零件 */
            $scope.delMateriel = function (item, index) {
                ybWidget.ShowMessage("确定要删除该记录吗？", MESG_TYPE.Ask).then(function () {
                    $scope.orderObj.borrowMaterialDetailDOList.splice(index, 1);
                    EventBus.Publish("setMaterial", item.materialNo);
                });
            };

            /* 批量删除零件 */
            $scope.batchDelMateriel = function () {
                var selectedCount = 0;
                for (var i = 0, len = $scope.orderObj.borrowMaterialDetailDOList.length; i < len; i++) {
                    if (!!$scope.orderObj.borrowMaterialDetailDOList[i].isChecked) {
                        selectedCount++;
                    }
                }
                if (!selectedCount) {
                    ybWidget.ShowMessage('请选择需要删除的行');
                    return;
                }
                ybWidget.ShowMessage("确定要删除选中的记录吗？", MESG_TYPE.Ask).then(function () {
                    for (var i = $scope.orderObj.borrowMaterialDetailDOList.length - 1; i >= 0; i--) {
                        if (!!$scope.orderObj.borrowMaterialDetailDOList[i].isChecked) {
                            $scope.orderObj.borrowMaterialDetailDOList.splice(i, 1);
                        }
                    }
                    if ($scope.orderObj.borrowMaterialDetailDOList.length === 0) {
                        $scope.markInfo.allCheckedMaterial = false;
                    }
                    EventBus.Publish("setMaterial");
                });
            };

            /* 零件数量合计 */
            $scope.countTotal = function (item) {
                item.qtyErr = false;
                if (!item.amount || parseFloat(item.amount) <= 0) {
                    item.qtyErr = true;
                }
                item.totalPrice = $filter("decimalCover")(ybWidget.Times(item.amount, item.price), 2);
            };

            /* 主列表的批量删除 */
            $scope.batchDel = function () {
                var deleteList = [];
                angular.forEach($scope.dataList, function (item) {
                    if (item.isChecked && item.oneLevelAuditStatus !== '1') {
                        deleteList.push(item.id);
                    } else if (item.isChecked && item.oneLevelAuditStatus === '1') {
                        deleteList = [];
                        return;
                    }
                });
                if (deleteList.length === 0) {
                    ybWidget.ShowMessage('请选择可删除的借用单（选择状态为未审核的借用单进行删除）', MESG_TYPE.Error);
                    return;
                }
                ybWidget.ShowMessage("确定要删除吗？", MESG_TYPE.Ask).then(function (data) {
                    var service = "device";
                    var reqUrl = "/borrowMaterial/batchDelete";
                    ybHttp.Post(service, reqUrl, deleteList).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        getDataList();
                    });
                })
            };


            /* 主列表单条删除 */
            $scope.delApply = function (id) {
                ybWidget.ShowMessage("确定要删除吗？", MESG_TYPE.Ask).then(function () {
                    var service = "device";
                    var reqUrl = "/borrowMaterial/delete";
                    ybHttp.Post(service, reqUrl, {
                        id: id
                    }).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        getDataList();
                    });
                })
            };

            /* 主列表新增 */
            $scope.addApply = function () {
                $scope.tabFlag.type = 1;
                $scope.tabFlag.name = tabFlag[1];
                $scope.orderObj.applicationDate = +new Date();
                $scope.orderObj.borrower = $scope.curUser.name; // 借用人获取当前登录人
                $scope.changeTab(2);
            };

            /* 主列表单条打印 */
            $scope.printSingle = function (item) {
                var param = {
                    printType: "BorrowIn",
                    cusParam: { ids: [item.id] }
                };
                ybWidget.SetIsPrintOrPreview(param);
            };

            /* 主列表多条打印 */
            $scope.batchPrint = function () {
                const billIds = [];
                for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                    if (!!$scope.dataList[i].isChecked) {
                        billIds.push($scope.dataList[i].id);
                    }
                }
                if (billIds.length === 0) {
                    ybWidget.ShowMessage('请选择需要打印的行');
                    return;
                }

                var param = {
                    printType: "BorrowIn",
                    cusParam: { ids: billIds }
                };
                ybWidget.SetIsPrintOrPreview(param);
            };


            /* 获取详情 */
            function getDetail(item) {
                var param = {
                    id: item.id
                };
                return ybHttp.Get('device', '/borrowMaterial/get', param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return false;
                    }
                    setMaterialDesc();
                    return resp.value;
                });
            }

            $scope.checkDate = function (item, start, end) {
                if ((angular.isUndefined(item[start]) || !item[start]) && (angular.isUndefined(item[end]) || !item[end])) {
                    return;
                }
                if (!!item[start] && (angular.isUndefined(item[end]) || !item[end])) {
                    item[start] = ybWidget.FormatStartDate(item[start]);
                    return;
                }
                if ((angular.isUndefined(item[start]) || !item[start]) && !!item[end]) {
                    item[end] = ybWidget.FormatEndDate(item[end]);
                    return;
                }
                var tmpTime = item[start];
                if (item[end] < item[start]) {
                    item[start] = ybWidget.FormatStartDate(item[end]);
                    item[end] = ybWidget.FormatEndDate(tmpTime);
                } else {
                    item[start] = ybWidget.FormatStartDate(item[start]);
                    item[end] = ybWidget.FormatEndDate(item[end]);
                }
            };

            /* 主列表编辑 */
            $scope.editApply = function (item) {
                getDetail(item).then(function (data) {
                    if (!data) {
                        return;
                    }
                    $scope.tabFlag.type = 2;
                    $scope.tabFlag.name = tabFlag[2];
                    $scope.orderObj = data;
                    $scope.orderObj.createdDate = new Date(data.createdDate);
                    $scope.orderObj.expectedArrivalDate = new Date(data.expectedArrivalDate);
                    $scope.orderObj.isBorrow = $filter("isBorrow")($scope.orderObj.isBorrow);
                    $scope.changeTab(2);
                });
            };

            function viewDetail(item) {
                getDetail(item).then(function (data) {
                    if (!data) {
                        return;
                    }
                    $scope.orderObj = data;
                    $scope.orderObj.isBorrow = $filter("isBorrow")($scope.orderObj.isBorrow);
                    $scope.orderObj.oneLevelAuditStatus = $filter("ProgressStatus")($scope.orderObj.oneLevelAuditStatus, "audit");
                    $scope.orderObj.twoLevelAuditStatus = $filter("ProgressStatus")($scope.orderObj.twoLevelAuditStatus, "audit");
                    $scope.changeTab(3);
                });
            }

            /* 主列表审核 */
            $scope.auditApply = function (item) {
                getDetail(item).then(function (data) {
                    if (!data) {
                        return;
                    }
                    $scope.orderObj = data;
                    $scope.changeTab(1);
                });
            };


            /* 选择零件 */
            $scope.selMateriel = function (e) {
                if (!!$scope.toolbarConfig.showMateriel) {
                    e.stopPropagation();
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig.showMateriel = true;
                        EventBus.Publish("getMaterial");
                    });
                }
            };

            $scope.$watch('orderObj.borrowMaterialDetailDOList.length', function () {
                setMaterialDesc();
            });

            function setMaterialDesc() {
                if (!$scope.orderObj.borrowMaterialDetailDOList || $scope.orderObj.borrowMaterialDetailDOList.length == 0) {
                    return;
                }
                angular.forEach($scope.orderObj.borrowMaterialDetailDOList, function (item) {
                    item.materialNo = item.materialsNo ? item.materialsNo : item.materialNo;
                    item.materialDesc = item.materialsDes ? item.materialsDes : item.materialDesc;
                });
            }

            $scope.saveData = function (type, invalid) {
                if (!invalid) {
                    $scope.submitted = true;
                    return;
                }
                var listValid = true;
                var service = "device";
                var reqUrl = "/borrowMaterial/add";
                if (!!$scope.orderObj.id) {
                    reqUrl = "/borrowMaterial/update";
                }
                if (!$scope.orderObj.borrowMaterialDetailDOList || $scope.orderObj.borrowMaterialDetailDOList.length == 0) {
                    ybWidget.ShowToaster('error', '零件不可为空');
                    return;
                }
                for (var i = 0, len = $scope.orderObj.borrowMaterialDetailDOList.length; i < len; i++) {
                    $scope.orderObj.borrowMaterialDetailDOList[i].qtyErr = false;
                    if (!$scope.orderObj.borrowMaterialDetailDOList[i].amount || parseFloat($scope.orderObj.borrowMaterialDetailDOList[i].amount) <= 0) {
                        $scope.orderObj.borrowMaterialDetailDOList[i].qtyErr = true;
                        listValid = false;
                        break;
                    }
                }
                if (!listValid) {
                    return;
                }
                setMaterialDesc();
                var param = angular.copy($scope.orderObj);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    $scope.changeTab(0);
                });
            };

            /* 审核通过 */
            $scope.auditPurchase = function (type, id, message) {
                var service = "device";
                var reqUrl = "/borrowMaterial/audit";
                if (type === "refuse") {
                    if (!message) {
                        ybWidget.ShowMessage('请输入审核意见');
                        $scope.submitted = true;
                        return;
                    }
                    reqUrl = "/borrowMaterial/refuse";
                }
                var param = {
                    id: id,
                    auditOpinion: message
                };
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    if (type === "pass") {
                        ybWidget.ShowMessage("审核成功!");
                    }

                    ybWidget.ShowToaster('success', '操作成功');
                    $scope.changeTab(0);
                    $scope.orderObj.auditOpinion = "";
                });
            };

            $scope.cancel = function () {
                $scope.changeTab(0);
            };

            /* 选择领用仓库 */
            $scope.selectWarehouse = function () {
                ybWidget.WarehouseSelector().then(function (data) {
                    if (!!$scope.orderObj.warehouseLeadIn && (data.warehouseLeadIn !== $scope.orderObj.warehouseLeadIn)) {
                        $scope.orderObj.warehouseAreaLeadIn = "";
                    }
                    $scope.orderObj.warehouseLeadIn = data.warehouseCode;
                })
            };

            /* 清除领用仓库 */
            $scope.clearWarehouse = function () {
                $scope.orderObj.warehouseLeadIn = "";
                $scope.orderObj.warehouseAreaLeadIn = "";
            };

            /* 选择库区 */
            $scope.selectArea = function () {
                if (!$scope.orderObj.warehouseLeadIn) {
                    ybWidget.ShowToaster('error', '请选择领入仓库');
                    return;
                }
                var param = {};
                param.warehouseCode = $scope.orderObj.warehouseLeadIn;
                ybWidget.DepotAreaSelector(param).then(function (data) {
                    $scope.orderObj.warehouseAreaLeadIn = data.areaCode;
                })
            };

            /* 清除库区 */
            $scope.clearArea = function () {
                $scope.orderObj.warehouseAreaLeadIn = "";
            }

            $scope.actionUrl = ybHttp.RequestUrl("device", "/borrowMaterial/download");

            function getBillIds() {
                $scope.exportParam.sectionParam.ids = [];
                angular.forEach($scope.dataList, function (item) {
                    if (!!item.isChecked) {
                        $scope.exportParam.sectionParam.ids.push(item.id);
                    }
                });
            }

            function checkAll(list) {
                var selectAll = false;
                for (var i = 0, len = list.length; i < len; i++) {
                    if (!list[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            $scope.selectSingle = function (item, list, type) {
                if (!item.isChecked) {
                    $scope.markInfo["allChecked" + type] = false;
                } else {
                    if (!checkAll(list)) {
                        $scope.markInfo["allChecked" + type] = false;
                    } else {
                        $scope.markInfo["allChecked" + type] = true;
                    }
                }
                getBillIds();
            };


            $scope.selectAll = function (list, type) {
                angular.forEach(list, function (item) {
                    item.isChecked = $scope.markInfo["allChecked" + type];
                });
                getBillIds();
            };

            (function () {
                getDataList();
                $scope.setFixedWidth();
                UserContextService.SetBtnPermission(setBtnPermission);
                if (!!$stateParams.guaApplyDetailVO) {
                    $scope.orderObj = $stateParams.guaApplyDetailVO.orderObj;
                    var operateNum = $stateParams.guaApplyDetailVO.operate
                    $scope.tabFlag.type = operateNum;
                    $scope.tabFlag.name = tabFlag[operateNum];
                    $scope.changeTab(operateNum);
                }
            })();
        }]);
})(window, window.angular);